//
//  ONRealTimeTableViewCell.m
//  Volatix/Data/Work/Volatix/Volatix/Volatix/ONDefine.h
//
//  Created by Anh Tran on 6/2/14.
//  Copyright (c) 2014 Familly. All rights reserved.
//

#import "ONRealTimeTableViewCell.h"

@implementation ONRealTimeTableViewCell

- (void)awakeFromNib
{
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


-(void)initRowWithNoDataUpdate:(ONISOObjects *)ISOObject andNumberColumn:(int)numberColumn andTag:(int)TAG
{
    CGFloat currentX = 0;
    for (int j=0; j<=_currentNumber_DayAhead; j++) {
        [[self viewWithTag:TAG+j] removeFromSuperview];
    }
    for (int j=0; j<=numberColumn; j++)
    {
        UILabel *valueLabel = [[UILabel alloc]initWithFrame:CGRectMake(currentX + 80*(j+1), 0, 60, 30)];
        valueLabel.textColor = [UIColor blackColor];
        valueLabel.font = [UIFont systemFontOfSize:13];
        valueLabel.textAlignment = NSTextAlignmentRight;
        BOOL isNormal=(j>=[ISOObject.columnValuesArray count])?true:false;
        valueLabel.text = (isNormal)?@"0.000":[ISOObject.columnValuesArray objectAtIndex:j];
        
        if(isNormal)
        {
            /// do nothing color
        }
        else
        {
           
            if ([valueLabel.text rangeOfString:@"↑"].location != NSNotFound) {
                
                [valueLabel setTextColor:kColorIncrease];//kColorIncrease
            }
            else if([valueLabel.text rangeOfString:@"↓"].location != NSNotFound)
            {
                [valueLabel setTextColor:kColorDescrease];//kColorDescrease
            }
//            else {
//                
//                float value=[valueLabel.text floatValue];
//                if(value>0)
//                {
//                    [valueLabel setTextColor:kColorIncrease];//kColorIncrease
//                }
//                else if(value<0)
//                {
//                    [valueLabel setTextColor:kColorDescrease];
//                }
//                else
//                {
//                    ///value=0; do nothing
//                }
//            }

        }
        valueLabel.tag = TAG+j;
        [self addSubview:valueLabel];
    }
    
    _currentNumber_DayAhead=numberColumn;
    // resize line
    CGRect newFrame = _lineView.frame;
    newFrame.size = CGSizeMake(320*(numberColumn/3), 1);
    _lineView.frame = newFrame;
  
}



-(void)loadCellData_DayAheadDetail:(ONISOObjects *)ISOObject WithDataUpdate:(ONObjectDayAhead  *)dataUpdate andNumberColumn:(int)numberColumn andShowArrow:(BOOL)value
{
   
     _backupISOObject=ISOObject;
    [_buttonClick setHidden:!value];
    _backupResult_DayAhead=dataUpdate;
    ///// temp code --> finding a misstake object .
    if(![_backupResult_DayAhead isKindOfClass:[ONObjectDayAhead class]])
    {
        NSLog(@"IS NULL OR NOT  ONObjectDayAhead class member");
        ISOObject.columnValuesArray = [[NSMutableArray alloc]init];
        for (int i=0; i<=numberColumn; i++) {
            [ISOObject.columnValuesArray addObject:[NSString stringWithFormat:@"0.000"]];
        }
       
    }else {
      
        ISOObject.columnValuesArray = [[NSMutableArray alloc]init];
        for (int i=0; i<=numberColumn; i++) {
            
            if (i==0) {
            //
                [ISOObject.columnValuesArray addObject:[NSString stringWithFormat:@"%.3f",(_backupResult_DayAhead==nil)?0:_backupResult_DayAhead.price]];
            }
            else if(i==1){
            //
                [ISOObject.columnValuesArray addObject:[NSString stringWithFormat:@"%.3f",(_backupResult_DayAhead==nil)?0:_backupResult_DayAhead.loss]];
            }
            else if(i==2){
            //
                [ISOObject.columnValuesArray addObject:[NSString stringWithFormat:@"%.3f",(_backupResult_DayAhead   ==nil)?0:_backupResult_DayAhead.congestion]];
            }
            else
            {
            /// dynamic column
                if(_backupResult_DayAhead!=nil)
                {
                    for (int index=0; index<[[_backupResult_DayAhead dynamicColumnData] count]; index++)
                    {
                        ONDataDynamicColumn * item =[[_backupResult_DayAhead dynamicColumnData] objectAtIndex:index];
                        if(ISOObject.name==item.nameColumn)
                        {
                            [ISOObject.columnValuesArray addObject:[NSString stringWithFormat:@"%.3f",(_backupResult_DayAhead==nil)?0:[item Value]]];
                        
                        }
                        else
                        {
                            [ISOObject.columnValuesArray addObject:[NSString stringWithFormat:@"0.000"]];
                        }
                    
                    
                    
                    
                    }
                }
                else
                {
                    [ISOObject.columnValuesArray addObject:[NSString stringWithFormat:@"0.000"]];
                
                }
            
            
            
            }
        
        }
    }
    BOOL isValue =![_backupResult_DayAhead isKindOfClass:[ONObjectDayAhead class]];
    self.titleLabel.text =[NSString stringWithFormat:@"%.3f",(isValue)?0:_backupResult_DayAhead.hourending/100];///---> remove /100 after server cal
    
    
    
    //[self.titleLabel setTextColor:(isValue)?[UIColor blackColor]:((_backupResult_DayAhead.hourending>0))?kColorIncrease:kColorDescrease];
    self.titleLabel.font = [UIFont systemFontOfSize:13];
    [self initRowWithNoDataUpdate:ISOObject andNumberColumn:numberColumn andTag:2000];
    // resize line
    CGRect newFrame = _lineView.frame;
    newFrame.size = CGSizeMake(320*(numberColumn/3), 1);
    _lineView.frame = newFrame;
}






-(void)loadCellData_DayAhead:(ONISOObjects *)ISOObject withDataUpdate:(NSMutableArray *)dataUpdate andCurrentChoiced:(NSInteger)choiced andNumberColumn:(int)numberColumn andShowArrow:(BOOL)value
{
    
    _backupISOObject=ISOObject;
    [_buttonClick setHidden:!value];
    for(int index =0; index<[dataUpdate count] ; index++)
    {
        ONObjectDayAhead * item= [dataUpdate objectAtIndex:index];
        switch (ISOObject.type) {
            case LEVEL_ISO:
                {
                    if([ISOObject.name isEqualToString:item.ISO])
                    {
                        _backupResult_DayAhead= item;
                    }
                }
                break;
            case LEVEL_LOADZONE:
                {
                    if([ISOObject.name isEqualToString:item.LOADZONE])
                    {
                        _backupResult_DayAhead= item;
                    }
                }
                break;
            case LEVEL_POWERPOINT:
                {
                    if([ISOObject.name isEqualToString:item.POWERPOINT])
                    {
                        _backupResult_DayAhead= item;
                    }
                
                }
                break;
            default:
                break;
        }
        
    }
    ///// temp code --> finding a misstake object .
    if(![_backupResult_DayAhead isKindOfClass:[ONObjectDayAhead class]])
    {
        NSLog(@"IS NULL OR NOT  ONObjectDayAhead class member");
        self.titleLabel.text = ISOObject.name;
        ISOObject.columnValuesArray = [[NSMutableArray alloc]init];
         for (int i=0; i<=numberColumn; i++) {
             [ISOObject.columnValuesArray addObject:[NSString stringWithFormat:@"0.000"]];
         }
    }
    else{
        ISOObject.columnValuesArray = [[NSMutableArray alloc]init];
        for (int i=0; i<=numberColumn; i++) {

                if (i==0) {
                           //
                    [ISOObject.columnValuesArray addObject:[NSString stringWithFormat:@"%.3f",(_backupResult_DayAhead==nil)?0:_backupResult_DayAhead.price]];
                }
                else if(i==1){
                //
                    [ISOObject.columnValuesArray addObject:[NSString stringWithFormat:@"%.3f",(_backupResult_DayAhead==nil)?0:_backupResult_DayAhead.loss]];
                }
                else if(i==2){
                //
                    [ISOObject.columnValuesArray addObject:[NSString stringWithFormat:@"%.3f",(_backupResult_DayAhead==nil)?0:_backupResult_DayAhead.congestion]];
                }
                else
                {
                /// dynamic column
                    if(_backupResult_DayAhead!=nil)
                    {
                        for (int index=0; index<[[_backupResult_DayAhead dynamicColumnData] count]; index++)
                            {
                                ONDataDynamicColumn * item =[[_backupResult_DayAhead dynamicColumnData] objectAtIndex:index];
                                if(ISOObject.name==item.nameColumn)
                                {
                                 //NSLog(@"aaaaaaaaaaaa");
                                    [ISOObject.columnValuesArray addObject:[NSString stringWithFormat:@"%.3f",(_backupResult_DayAhead==nil)?0:[item Value]]];
                                 
                                }
                                else
                                {
                                    [ISOObject.columnValuesArray addObject:[NSString stringWithFormat:@"0.000"]]   ;
                                }
                            }
                    }
                    else
                    {
                        [ISOObject.columnValuesArray addObject:[NSString stringWithFormat:@"0.000"]];
                        
                    }
                


                }
        }
        
    }
    
    self.titleLabel.text = ISOObject.name;
    [self initRowWithNoDataUpdate:ISOObject andNumberColumn:numberColumn andTag:1000];
    // resize line
    CGRect newFrame = _lineView.frame;
    newFrame.size = CGSizeMake(320*(numberColumn/3), 1);
    _lineView.frame = newFrame;
}
    
    



- (void)loadCellData_RealTime:(ONISOObjects*)ISOObject withDataUpdate:(NSMutableArray *)dataUpdate andCurrentChoiced :(NSInteger)choiced andNumberColumn:(int)numberColumn andShowArrow:(BOOL)value{
    // templace text
    //@"↓99";//@"↑77"
    _backupISOObject=ISOObject;
    [_buttonClick setHidden:!value];
    
    for(int index =0; index<[dataUpdate count] ; index++)
    {
        ONObjectRealTime * item =[dataUpdate objectAtIndex:index];
        switch (ISOObject.type) {
            case LEVEL_ISO:
                //
                if([ISOObject.name isEqualToString:item.ISO])
                {
                    _backupResult= item;
                    break;
                }
                
                break;
            case LEVEL_LOADZONE:
                //
                if([ISOObject.name isEqualToString:item.LOADZONE])
                {
                    _backupResult= item;
                    break;
                }

                break;
            case LEVEL_POWERPOINT:
                //
                if([ISOObject.name isEqualToString:item.POWERPOINT])
                {
                    _backupResult= item;
                    break;
                }
                break;
                
            default:
                break;
        }
        
        
    }
    
    
    // input value
    ISOObject.columnValuesArray = [[NSMutableArray alloc]init];
    for (int i=0; i<=numberColumn; i++) {

        if(i==0)
        {
            switch (choiced) {
                case HOURLY:
                    //
                    //[ISOObject.columnValuesArray addObject:[NSString stringWithFormat:@"%@%.3f",(_backupResult==nil)?@"":(_backupResult.isHourlyHighIncreased)?@"↑":@"↓",(_backupResult==nil)?0:_backupResult.hourlyHigh]];
                    [ISOObject.columnValuesArray addObject:[NSString stringWithFormat:@"%.3f",(_backupResult==nil)?0:_backupResult.hourlyHigh]];
                    
                    break;
                case DAILY:
                   // [ISOObject.columnValuesArray addObject:[NSString stringWithFormat:@"%@%.3f",(_backupResult==nil)?@"":(_backupResult.isDailyHighIncreased)?@"↑":@"↓",(_backupResult==nil)?0:_backupResult.dailyHigh]];
                    [ISOObject.columnValuesArray addObject:[NSString stringWithFormat:@"%.3f",(_backupResult==nil)?0:_backupResult.dailyHigh]];
                    break;
                case WEAKLY:
                    //[ISOObject.columnValuesArray addObject:[NSString stringWithFormat:@"%@%.3f",(_backupResult==nil)?@"":(_backupResult.isWeeklyHighIncreased)?@"↑":@"↓",(_backupResult==nil)?0:_backupResult.weeklyHigh]];
                    [ISOObject.columnValuesArray addObject:[NSString stringWithFormat:@"%.3f",(_backupResult==nil)?0:_backupResult.weeklyHigh]];
                    break;
                case MONTHLY:
                    //[ISOObject.columnValuesArray addObject:[NSString stringWithFormat:@"%@%.3f",(_backupResult==nil)?@"":(_backupResult.isMonthlyHighIncreased)?@"↑":@"↓",(_backupResult==nil)?0:_backupResult.monthlyHigh]];
                    [ISOObject.columnValuesArray addObject:[NSString stringWithFormat:@"%.3f",(_backupResult==nil)?0:_backupResult.monthlyHigh]];
                    break;
                    
                    
                default:
                    break;
            }

        }
        else if(i==1)
        {
            switch (choiced) {
                case HOURLY:
                    //[ISOObject.columnValuesArray addObject:[NSString stringWithFormat:@"%@%.3f",(_backupResult==nil)?@"":(_backupResult.isHourlyLowIncreased)?@"↑":@"↓",(_backupResult==nil)?0:_backupResult.hourlyLow]];
                    [ISOObject.columnValuesArray addObject:[NSString stringWithFormat:@"%.3f",(_backupResult==nil)?0:_backupResult.hourlyLow]];
                    break;
                case DAILY:
                     //[ISOObject.columnValuesArray addObject:[NSString stringWithFormat:@"%@%.3f",(_backupResult==nil)?@"":(_backupResult.isDailyLowIncreased)?@"↑":@"↓",(_backupResult==nil)?0:_backupResult.dailyLow]];
                    [ISOObject.columnValuesArray addObject:[NSString stringWithFormat:@"%.3f",(_backupResult==nil)?0:_backupResult.dailyLow]];
                    break;
                case WEAKLY:
                   // [ISOObject.columnValuesArray addObject:[NSString stringWithFormat:@"%@%.3f",(_backupResult==nil)?@"":(_backupResult.isWeeklyLowIncreased)?@"↑":@"↓",(_backupResult==nil)?0:_backupResult.weeklyLow]];
                    [ISOObject.columnValuesArray addObject:[NSString stringWithFormat:@"%.3f",(_backupResult==nil)?0:_backupResult.weeklyLow]];
                    break;
                case MONTHLY:
                    //[ISOObject.columnValuesArray addObject:[NSString stringWithFormat:@"%@%.3f",(_backupResult==nil)?@"":(_backupResult.isMonthlyLowIncreased)?@"↑":@"↓",(_backupResult==nil)?0:_backupResult.monthlyLow]];
                    [ISOObject.columnValuesArray addObject:[NSString stringWithFormat:@"%.3f",(_backupResult==nil)?0:_backupResult.monthlyLow]];
                    
                    break;
                    
                    
                default:
                    break;
            }

        }
        else if(i==2)
        {
            
            [ISOObject.columnValuesArray addObject:[NSString stringWithFormat:@"%@%.3f",(_backupResult==nil||[_backupResult current]==0)?@"":(_backupResult.isCurrentIncreased)?@"↑":@"↓",(_backupResult==nil)?0:_backupResult.current]];
            
        }
        else
        {
            /// dynamic column
            if(_backupResult!=nil)
            {
                for (int index=0; index<[[_backupResult dynamicColumnData] count]; index++) {
                    ONDataDynamicColumn * item =[[_backupResult dynamicColumnData] objectAtIndex:index];
                    if(ISOObject.name==item.nameColumn)
                    {
                        [ISOObject.columnValuesArray addObject:[NSString stringWithFormat:@"%@%.3f",([item Value]==0)?@"":(item.isInscrease)?@"↑":@"↓",[item Value]]];
                    }
                    else
                    {
                        [ISOObject.columnValuesArray addObject:[NSString stringWithFormat:@"0.000"]];
                    }
                    
                }
            
               
            }
            else
            {
                [ISOObject.columnValuesArray addObject:[NSString stringWithFormat:@"0.000"]];
            }
        }
        
    }
    
    // input value
    /**
     * 1. h/d/w/m hight
     * 2. h/d/w/m low
     * 3. Current
     */
     self.titleLabel.text = ISOObject.name;
    [self initRowWithNoDataUpdate:ISOObject andNumberColumn:numberColumn andTag:100];
    // resize line
    CGRect newFrame = _lineView.frame;
    newFrame.size = CGSizeMake(320*(numberColumn/3), 1);
    _lineView.frame = newFrame;
}





- (IBAction)clickChangeScene:(id)sender {
    if(_delegate!=nil)
    {
        [_delegate clickNext:_backupISOObject];
    }
}

@end
